<?php

namespace App\Services\SpiderContent;

use Carbon\Carbon;
use App\Models\User;
use App\Models\News;
use App\Jobs\RewardFirstParentJob;
use Illuminate\Support\Facades\Log;
use Rainsens\Dinglog\Facades\Dinglog;
use Illuminate\Database\Eloquent\Collection;
use Sunra\PhpSimple\HtmlDomParser;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Nesk\Puphpeteer\Puppeteer;

use DOMDocument;


class SpiderScmpContent
{

    public function execute($baseUrl)
    {
    	ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)');

    	$puppeteer = new Puppeteer; // 新建 Puppeteer 实例

         $browser = $puppeteer->launch(['args'=>['--no-sandbox']]); // 启动无头浏览器
         $page = $browser->newPage(); // 打开新的标签页
       
         $page->tryCatch->goto($baseUrl,[
             'timeout' => 0,
             'read_timeout' => 30

         ]); // 访问页面
        	
       	 
         $html = $page->content();
       
        
        /* return $html; // 返回 js 渲染后的页面
        
		$html = Browsershot::url($baseUrl1)->setDelay(1000)->bodyHtml();*/
		
		

    	//$html = '<a href="aaaa"><div class="content-item"><div>测试</div></div></a>';
    	$dom = new DOMDocument();
    	

    	libxml_use_internal_errors(true);
    	//$html = htmlspecialchars($html);
    	
		
        $dom->loadHTML('<?xml encoding="UTF-8">' . $html);
      
        $elements = $dom->getElementsByTagName('div');

     	//$html = HtmlDomParser::str_get_html($html);

     	//dd($elements->nodeValue);

     	$data = [];
     	$i = 0;

     	$hoursMatch = "/\d+ hours ago/";
     	$MinutsMatch = "/\d+ minutes ago/";
        foreach($elements as $key=>$val){
        	$url = "";

    		if ((strpos($val->getAttribute('data-qa'), 'SubSection-HeroArticles') !== false )) {
    			
    	 		$content = $dom->saveHTML($val);

    	 		
    	 		if(strstr($content,'minutes ago')){

    	 			$data[$i]= ["images"=>"","brief_zn"=>'',"content_zn"=>"","title_zn"=>"","images"=>"",'source'=>'scmp',"created_at"=>Carbon::now(),"url"=>"","started_at"=>Carbon::now()];
    	 			$timeMatchContent  = preg_match($hoursMatch,$content,$match);

    	 			if(!empty($match)){
	    	 			preg_match('/\d+/',$match[0],$times);
	    	 			if(!empty($times)){

	    	 				$matchTime = $times[0];
	    	 				$data[$i]['started_at'] = Carbon::now()->subHours($matchTime);

	    	 			}

	    	 		}else{
	    	 			preg_match($MinutsMatch,$content,$match);
	    	 			if(!empty($match)){
	    	 				preg_match('/\d+/',$match[0],$times);
	    	 			}
	    	 			if(!empty($times)){

	    	 				$matchTime = $times[0];
	    	 				$data[$i]['started_at'] = Carbon::now()->subMinutes($matchTime);
	    	 				
	    	 			}	

	    	 		}

	    	 		if(strpos($val->getAttribute('class'), 'article-level-three') !== false){
	    	 			$newStr = str_replace(" ", "", $val->childNodes[1]->textContent);
	    	 			if(empty($newStr)){

	    	 				$childNode = $val->childNodes[2]->childNodes[0];
	    	 			}else{
	    	 				$childNode = $val->childNodes[1]->childNodes[0];
	    	 				

	    	 			}
	    	 			
	    	 			$srcNode = $childNode->childNodes[0];
	    	 			
	    	 		}else{

	    	 			$childNode = $val->childNodes[0]->childNodes[0]->childNodes[0]->childNodes[0]->childNodes[0];
	    	 			$srcNode = $childNode->childNodes[0]->childNodes[0]->childNodes[0];
	    	 		}
	    	 		
	    	 		//dd($dom->saveHTML($srcNode));
	    	 		/*if(empty($childNode)){
	    	 			$childNode = $val->childNodes[0]->childNodes[0]->childNodes[0]->childNodes[0]->childNodes[0]->childNodes[0]->childNodes[0]->childNodes[0]->childNodes[0];

	    	 		}*/
	    	 		$attrs= $childNode->attributes;
	    	 		
	    	 		$url = "";
	    	 		foreach($attrs as $attr){
	    	 			if($attr->nodeName =="href"){

    	 					$data[$i]['url'] = "https://www.scmp.com".$attr->textContent;
    	 					
    	 				}
	    	 			
	    	 		}
	    	 		
	    	 		
	    	 		//$srcNode = $childNode->childNodes[0]->childNodes[0]->childNodes[0];
	    	 		//dd($dom->saveHTML($srcNode));
	    	 		$img= "";

	    	 		if($srcNode){
						$srcs= $srcNode->attributes;

		    	 		foreach($srcs as $src){
		    	 			if($src->nodeName =="srcset"){

	    	 					$img = $src->textContent;
	    	 					
	    	 				}
		    	 			
		    	 		}
	    	 		}


	    	 		if($img){

	    	 			ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)');

    					$imageContent = $this->getUrl($img);

						$newFileName = "/images/".Str::random(40) . '.jpg'; // 假设是JPG格式
						Storage::disk('oss')->put($newFileName, $imageContent);
						
						// 你现在可以通过存储的路径访问图片
						$data[$i]['images'] = Storage::disk('oss')->path($newFileName);
						
    				}
    				
			       
    				 $page->tryCatch->goto($data[$i]['url'],[
			             'timeout' => 0,
			             'read_timeout' => 120,
			             'idle_timeout' => 120

			         ]); // 访问页面

			         $contentHtml = $page->content();
    				//$contentHtml = file_get_contents($data[$i]['url']);
			         
	        	
		        	$contentDom = new DOMDocument();

		        	$contentDom->loadHTML('<?xml encoding="UTF-8">' . $contentHtml);
		        	$Titleelements = $contentDom->getElementsByTagName('h2');

		        	 foreach($Titleelements as $titleKey=>$titleVal){

			        	if (strpos($titleVal->getAttribute('data-qa'), 'ContentHeadline-Container') !== false) {
	        			
	        				$data[$i]['title_zn'] = $titleVal->textContent;
	        				


	        			}
	        		}

	        		$Contentelements = $contentDom->getElementsByTagName('div');

		        	 foreach($Contentelements as $contentKey=>$contentVal){

			        	if (strpos($contentVal->getAttribute('data-qa'), 'GenericArticle-Content') !== false) {
	        			
	        				$data[$i]['content_zn'] = $contentDom->saveHTML($contentVal);//$contentVal->textContent;

	        				$data[$i]['content_zn'] = preg_replace("/\{\"@context\".*?\}/", "\n" , $data[$i]['content_zn']);


	        			}
	        		}

	        		$i++;
	        		
    	 		}	
	        		
    		 }


        	
        	  
		}

		$browser->close();
		DB::table('deep_news')->insert($data);
		//dd($data);
     	 
    	//dd($elements);

     	//dd(retrieve_url_contents($url));
     	//$html = file_get_contents('https://www.huxiu.com/club/1000.html?object_type=51&object_id=1');
     	//dd($html);
 
		
    }


    public function djNodes($nodes,$res)
    {
    	if($nodes->nextSibling && $nodes->nextSibling->nodeName!="h4"){

    		$res['content'] = $res['content'].$nodes->nextSibling->textContent;


    		if($nodes->nextSibling->childNodes){

    			foreach($nodes->nextSibling->childNodes as $nn){
    				if($nn->nodeName=="img"){
    					$res['image'] = $nn->getAttribute('src');
    				}
    			}
    		}
    		
    		if(isset($nodes->nextSibling->nextSibling)  && $nodes->nextSibling->nextSibling->nodeName != "h4"){

    			 $res = $this->djNodes($nodes->nextSibling,$res);
    		}
    	}

    	return  $res;
    	//dd($nodes->nextSibling->nextSibling);
    	

    }

	function getUrl($url) {
	    // 初始化cURL

	   $headers[] = "Content-type: application/x-www-form-urlencoded";

    	$curl = curl_init(); // 启动一个CURL会话

		curl_setopt($curl, CURLOPT_URL, $url);

		curl_setopt($curl, CURLOPT_HEADER, 0);

		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查

		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在

		curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		 
		// 执行cURL会话
		$html = curl_exec($curl);
	    
	    return $html;
	}

	public function __invoke()
	{
		$baseUrl1 = "https://www.scmp.com/tech/big-tech";
		//Dinglog::text("========== refresh seckill product num start ......");

		//$baseUrl1="http://www.xbreakingtechinsight.com/";
		  
		
		$this->execute($baseUrl1);

		//Dinglog::text("========== refresh seckill product num end ......");
	}
}
